# todo 去废弃
import datetime

# from app.customer.model import Customer
# from app.warehouse.model import Warehouse
from common.base_model import BaseModel, BasicModelMixin
from exts import db
from common import constant


class Order(BasicModelMixin, db.Model, BaseModel):
    __abstract__ = True

    id = db.Column(db.Integer, primary_key=True)
    order_sn = db.Column(db.String(constant.MIDDLE_DB_STR_LEN))
    # order_type = db.Column(default=constant.OrderType.PURCHASE, nullable=False)
    # 单据日期
    date = db.Column(db.Date, default=datetime.datetime.utcnow)
    status = db.Column(db.Integer, default=constant.OrderStatus.DRAFT.value, nullable=False)
    draft_status = db.Column(db.Integer, default=constant.OrderDraftStatus.PENDING.value, nullable=False)
    # order_items = db.relationship("OrderItem", back_populates='order', cascade="all, delete, delete-orphan")


class PurchaseSaleOrder(Order):
    __abstract__ = True
    customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'))
    warehouse_id = db.Column(db.Integer, db.ForeignKey('warehouse.id'))
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))  # 结算账户
    second_account_id = db.Column(db.Integer, db.ForeignKey('account.id'))  # 结算账户

    # 快递单号
    tracking_number = db.Column(db.String(constant.LONG_DB_STR_LEN))
    # 是否已开票
    is_make = db.Column(db.Boolean, default=False, server_default="False")
    bill_info = db.Column(db.String(constant.LONG_DB_STR_LEN))

    # relation
    customer = db.relationship('Customer')
    warehouse = db.relationship("Warehouse")
    account = db.relationship("Account", foreign_keys='Order.account_id')
    second_account = db.relationship("Account", foreign_keys='Order.second_account_id')


class PurchaseReturnSaleOrder(PurchaseSaleOrder):
    __tablename__ = 'purchase_return_order'

    income = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=0)  # 实收金额
    second_income = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=0)  # 实收金额

    # 关联采购单
    related_order_id = db.Column(db.Integer, db.ForeignKey('purchase_order.id'))
    order_skus = db.relationship("PurchaseReturnOrderSku", back_populates='order', cascade="all, delete, delete-orphan")


class ReturnReturnSaleOrder(PurchaseSaleOrder):
    __tablename__ = 'sale_return_order'

    payment = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=0)  # 实付金额
    second_payment = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=0)  # 实付金额
    # 关联采购单
    related_order_id = db.Column(db.Integer, db.ForeignKey('sale_order.id'))
    order_skus = db.relationship("SaleReturnOrderSku", back_populates='order', cascade="all, delete, delete-orphan")


class TransferOrder(Order):
    __tablename__ = 'transfer_order'

    from_warehouse_id = db.Column(db.Integer, db.ForeignKey('warehouse.id'))
    to_warehouse_id = db.Column(db.Integer, db.ForeignKey('warehouse.id'))

    order_skus = db.relationship("TransferOrderSku", back_populates='order', cascade="all, delete, delete-orphan")


# ------------ 单据商品详情 -------------
class OrderSku(BasicModelMixin, db.Model, BaseModel):
    __abstract__ = True

    sku_id = db.Column(db.Integer, db.ForeignKey('sku.id'))
    num = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=0)  # 数量
    price = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=0)  # 单价
    discount = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=1, server_default='1')  # 折扣
    sku = db.relationship("Sku")

    @property
    def total_price(self):
        return self.price * self.num * self.discount

    @property
    def real_price(self):
        return self.price * self.discount


class PurchaseReturnOrderSku(OrderSku):
    __tablename__ = 'purchase_return_order_sku'

    order_id = db.Column(db.Integer, db.ForeignKey('purchase_return_order.id'))


class SaleReturnOrderSku(OrderSku):
    __tablename__ = 'sale_return_order_sku'

    order_id = db.Column(db.Integer, db.ForeignKey('sale_return_order.id'))


class TransferOrderSku(OrderSku):
    __tablename__ = 'transfer_order_sku'

    # # 调出仓库当前数量 (干什么用的？库存流水不应该有记录吗？)
    # from_before_num = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=0)
    # # 调出仓库当前数量
    # to_before_num = db.Column(db.DECIMAL(20, constant.COMMON_DECIMAL), default=0)
